package app.web.cartoon.analyse.detail;

import java.util.HashMap;
import java.util.Map;

import app.web.spe.CartoonSPE;
import cn.lily.com.component.schema.table.multi.TMultiCell;
import cn.lily.com.component.schema.table.multi.auto.TMultiView;
import cn.lily.com.component.schema.table.multi.item.TMultiBody;
import cn.lily.com.component.schema.table.multi.item.TMultiTitle;
import cn.lily.com.container.data.ListTable;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.grid.TMultiGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 动漫首字母分析页面
 * @author JiaJ
 * @date 2017年12月3日
 *
 */
public class CartoonMemo extends BusinessPage {
	
	private Map<String, ListTable> dataMap = new HashMap<String, ListTable>();

	@Override
	public void page_onLoad() throws YaconException {
		
		TMultiGrid cartoonMemoGrid = new TMultiGrid("cartoonMemoGrid");
		
		this.instData();
		
		this.instGrid(cartoonMemoGrid);
		
		this.instData(cartoonMemoGrid);
		
		this.getPage().addElement(cartoonMemoGrid);
			
	}
	
	/**
	 * 初始化数据
	 */
	public void instData() {
		
		this.dataMap.clear();
		
		String sql = "select CAN_DETAIL_ID,CAN_DETAIL_NAME,to_number(CAN_TOTAL) CAN_TOTAL,"
				          + "to_char(CAN_TIME,'yyyy-mm-dd hh24:mi:ss') CAN_TIME_NOW "
				   + "from ${schema}.LEO_CAN_DETAIL_INFO ";
		sql = "select * from ("+sql+") aa order by aa.CAN_TOTAL DESC";
		
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		while(dt.next()){
			
			String time = dt.getString("CAN_TIME_NOW").substring(0,4);
			String canID = dt.getString("CAN_DETAIL_ID");
			int canTotal = dt.getInt("CAN_TOTAL");
			String canName = dt.getString("CAN_DETAIL_NAME");
			
			ListTable activeTable = this.dataMap.get(time);
			if(activeTable == null){
				activeTable = new ListTable(time);
				this.dataMap.put(time, activeTable);
			}
			
			Record activeRecord = activeTable.createRecord();
			activeRecord.setFieldValue("CAN_TIME", time);
			activeRecord.setFieldValue("CAN_TOTAL", canTotal);
			activeRecord.setFieldValue("CAN_ID", canID);
			activeRecord.setFieldValue("CAN_NAM", canName);
		
		}
		
	}
	
	/**
	 * 初始化标题
	 * @param cartoonFanGrid
	 */
	public void instGrid(TMultiGrid cartoonWordGrid){
		
		TMultiTitle title = cartoonWordGrid.getTitle();
		
		title.createCell(0, 0, 1, 5).setFieldName("detailInfo").setFieldValue("动漫详情表");
			
		/* 设置标题 */
		TButtonPilot cartoonMemoPilot = ButtonPilot("cartoonMemoPilot");
		cartoonMemoPilot.setPilotIco("/widget/buttonPilot.gif");
		cartoonMemoPilot.setPilotText("动漫详情信息");
		cartoonMemoPilot.addButtons(new TButton("refresh"));
		
		cartoonWordGrid.setButtonPilot(cartoonMemoPilot);
		
		//设置双击事件
		cartoonWordGrid.setDataDbClick("cartoonMemo_dbclick");
		
	}
	
	/**
	 * 数据加载
	 * @param testGrid
	 */
	public void instData(TMultiGrid testGrid){

		TMultiBody body = testGrid.getBody();
		
		int rowIdx = -1;
		int colIdx = 0;
		
		int startYear = CartoonSPE.MAX_YEAR;
		int endYear = CartoonSPE.MIN_YEAR;
		
		for(;startYear>=endYear;startYear--){
			
			ListTable table = this.dataMap.get(startYear+"");
			
			rowIdx++;
			colIdx = 0;
			
			if(table == null){
				
				body.createDataCell(rowIdx, 0,1,5).setFieldValue(startYear+"年（共0部）");
				
				this.setTitleColor(body,rowIdx);
		
				rowIdx++;	
				
				this.setBlankColor(body, rowIdx);
				
				continue;
			}else{
				
				body.createDataCell(rowIdx, 0,1,5).setFieldValue(startYear+"年（共"+table.getRecordCount()+"部）");
				
				this.setTitleColor(body,rowIdx);
				
			}
			
			table.beforeFirst();
			while(table.next()){
				
				if(colIdx % 5 == 0){
					rowIdx++;
				}
				
				body.createDataCell(rowIdx, colIdx % 5).setFieldValue(table.getString("CAN_NAM")+"-"+table.getString("CAN_TOTAL"));
						
				this.setColor(body.getDataCell(rowIdx, colIdx % 5), table.getInt("CAN_TOTAL"));
				colIdx++;
				
			}
			
			int result = colIdx % 5;
			if(result != 0){
				for(;result<5;result++){

					this.setColor(body.createDataCell(rowIdx, result), -1);
				}
			}
	
			rowIdx++;	
			this.setBlankColor(body, rowIdx);
				
		}
		
	}
	
	public void setTitleColor(TMultiBody body,int rowIdx){
		
		body.getDataCell(rowIdx, 0).setBackColor("#D6D6D6").setFontColor("#000000");
		
	}
	
	public void setBlankColor(TMultiBody body,int rowIdx){
		
		body.createDataCell(rowIdx, 0 , 1 , 5).setFieldValue("&nbsp;").setBackColor("#FFFFFF");
		
	}
	
	public void setColor(TMultiCell cell,int count){
		
		if(count < 0){
			cell.setBackColor("#FFFFFF");
			cell.setFontColor("#000000");
		}
		
		//超短篇 -- 绿色
		else if(count>=0 && count < 4){
			cell.setBackColor("#CAFF70");
			cell.setFontColor("#000000");
		}
		
		//季番 -- 蓝色
		else if(count >=4 && count<16){
			cell.setBackColor("#E0FFFF");	
			cell.setFontColor("#000000");
		}
		
		//半年番 -- 黄色
		else if(count >=16 && count < 31){
			cell.setBackColor("#FFFF00");
			cell.setFontColor("#000000");
		}
		
		//年番 --橙色
		else if(count >=31 && count < 75){
			cell.setBackColor("#FF8C00");
			cell.setFontColor("#000000");
		}
		
		//两年番 -- 粉色
		else if(count >=75 && count < 129){
			cell.setBackColor("#FF83FA");
			cell.setFontColor("#000000");		
		}
		
		//长篇 -- 红色
		else{
			cell.setBackColor("#FF3030");
			cell.setFontColor("#000000");
		}
	}

	/**
	 * null转换为空字符串
	 * @param str
	 * @return
	 */
	public String getString(String str){
		if(str == null){
			return "";
		}
		return str;
	}
	

	public void refresh() throws YaconException {
		
		TMultiGrid cartoonMemoGrid = new TMultiGrid("cartoonMemoGrid");
		
		this.instData();
		
		this.instGrid(cartoonMemoGrid);
		
		this.instData(cartoonMemoGrid);
			
		this.setSelfMessage(cartoonMemoGrid.getDataElement());
		
	}

}
